$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'dictionary/dictionary/searchQuery'+vm.requestParameter(),
        datatype: "json",
        colModel: [			
			{ label: '数据字典ID', name: 'dictionaryid', index: 'DictionaryId', width: 50, key: true },
			{ label: '字典类型', name: 'typename', index: 'TypeName', width: 80 },
            { label: '数据类型名称', name: 'datatypename', index: 'DataTypeName', width: 80 },
			{ label: '字典内容', name: 'dictionaryvalue', index: 'DictionaryValue', width: 80 }, 			
			{ label: '字典中文内容', name: 'dictionaryvalueinzhcn', index: 'DictionaryValueInZhCN', width: 80 }, 			
			{ label: '序号', name: 'optionorder', index: 'OptionOrder', width: 50 },
			{ label: '创建时间', name: 'createtime', index: 'CreateTime', width: 90 },
			{ label: '更新时间', name: 'updatetime', index: 'UpdateTime', width: 90 },
			{ label: '软删除', name: 'isdel', index: 'IsDel', width: 50 , formatter: function(value, options, row){
                    return value === 0 ?
                        '<span class="label label-success">正常</span>' :
                        '<span class="label label-danger">已删除</span>';
                }},
            { label: '是否只读', name: 'isreadonly', index: 'IsReadOnly', width: 40, formatter: function(value, options, row){
                    return value === 0 ?
                        '<span class="label label-success">可写</span>' :
                        '<span class="label label-danger">只读</span>';
                } }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
            localStorage.removeItem("typename");
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		typenames:[
			{typename:"sex",}
		],
        typename:null,
		showList: true,
		title: null,
		dictionary: {}
	},
    beforeMount:function () {
        $.ajax({
            type: "GET",
            url: baseURL + 'dictionary/dictypes/queryTypeNames',
            contentType: "application/json",
            success: function(r){
                if(r.code === 0){
                    vm.typenames = r.data;
                }else{

                }
            }
        });
    }
	,
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.dictionary = {
				isdel:'0',
			};
		},
		update: function (event) {
			var dictionaryid = getSelectedRow();
			if(dictionaryid == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(dictionaryid)
		},
		saveOrUpdate: function (event) {
			var url = vm.dictionary.dictionaryid == null ? "dictionary/dictionary/save" : "dictionary/dictionary/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.dictionary),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var dictionaryids = getSelectedRows();
			if(dictionaryids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "dictionary/dictionary/delete",
                    contentType: "application/json",
				    data: JSON.stringify(dictionaryids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(dictionaryid){
			$.get(baseURL + "dictionary/dictionary/info/"+dictionaryid, function(r){
                vm.dictionary = r.dictionary;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
				url:baseURL + 'dictionary/dictionary/searchQuery'+((vm.typename != null)? "?typename="+vm.typename : ""),
				postData:{"typename":vm.typename},
                page:page
            }).trigger("reloadGrid");
			vm.typename = null;
		},
		requestParameter:function () {
			var str = localStorage.getItem("typename");
			return str != null ? "?typename="+str: "" ;
        }
	}
});